import {getBargainOwnOrOtherInfo} from '../../../Tools/HTTPOpertion.js';
var timer = require('../../../components/wxTimer/wxTimer.js')
Page({
  data: {
    userId: '',
    showModal: false,
    showRuleModal: false,
    closeIcon: 'https://mpic.denong.com/xcx-c-20180703-close.png',
    cutPrice: 0,
    hasPrice: 0,
    totalCutPrice: 0,
    barginOwnData: {
    },
    yanglao: '',
    groupPrice: '',
    wxTimerList: {},
    id: '',
    intPara: '',
  },
  onLoad: function (options) {
    console.log(options)
    // 请求数据
    const id = options.id;
    const intPara =  options.intPara;
    console.warn(id)
    console.warn(intPara)
    this.setData({
      id:id,
      intPara: intPara,
      userId: wx.getStorageSync('userId'),
    })
    this.loadData(id,intPara);
  },
  navToGoodDetail: function() {
    console.log('click', this.data);
    const id = this.data.barginOwnData.orderId;
    wx.navigateTo({
        url: `/pages/active/BargainDetails/BargainDetails?id=${id}`,
    })
  },
  loadData: function (id, intPara) {
    var self = this;
    const params = {
      token: wx.getStorageSync('token'),
      cnd: id,
      intPara,
    };
    getBargainOwnOrOtherInfo(params).then(function (result) {
      if (result.code === 0) {
        self.setData({ barginOwnData: result.data });
        if (result.data.showFlag == 0) {
          self.setData({ showModal: false });
        } else {
          self.setData({ showModal: true });
        }

        const cutPrice = ((result.data.now_price - result.data.group_price) / 100).toFixed(2)
        const totalCutPrice = ((result.data.price - result.data.now_price) / 100).toFixed(2)
        const hasPrice = ((result.data.now_price -  result.data.group_price) / 100).toFixed(2)
        // const xiaojin = (result.data.price / 100 * result.data.ratio / 100).toFixed(2)
        // const xiaojin = result.data.price / 100 * result.data.ratio / 100
        const xiaojin = (result.data.group_price * result.data.ratio / 100) > 1 ? (result.data.group_price * result.data.ratio / 10000).toFixed(2) : 0.01;
        const yanglao = (result.data.not_deposite / 100 + result.data.depositing / 100).toFixed(2)
        const groupPrice = (result.data.group_price / 100).toFixed(2)

        console.log(cutPrice, totalCutPrice, hasPrice)
        self.setData({
          cutPrice,
          totalCutPrice,
          hasPrice,
          'barginOwnData.xiaojin': xiaojin,
          yanglao,
          groupPrice
        })

        var wxTimer = new timer({
            beginTime: result.data.deadLine,
            name: 'wxTimer1',
            complete:function(){
                console.log("完成了")
            }
        })
        wxTimer.start(self);
        wx.hideLoading();
      } else if (result.code == -3) {
        wx.showToast({
          title: '该商品已下架',
          icon: 'none',
          duration: 20000,
        });
        setTimeout(function () {
          wx.navigateBack();
        }, 2000);
      } else {
        wx.showToast({
          title: result.message,
          icon: 'none',
          duration: 2000,
        });
      }
    }).catch(function (error) {
        console.log(error);
        wx.hideLoading()
    });
  },
  onShareAppMessage: function () {
    console.log('id:', this.data.barginOwnData.goods_group_id);
      console.log('intPara:', this.data.barginOwnData.group_buy_id);
    const self = this;
    return {
      title: '砍价',
      path: `/pages/active/bargainGivenPage/bargainGivenPage?id=${this.data.barginOwnData.goods_group_id}&intPara=${this.data.barginOwnData.group_buy_id}`,
      success: function(res) {
        // 转发成功
        wx.showToast({
          title: '转发成功',
          duration: 2000
        })
        self.setData({ showModal: false })
      },
      fail: function(res) {
        // 转发失败
        wx.showToast({
          title: '转发失败',
          duration: 2000
        })
      }
    }
  },
  showModalBtn: function () {
    console.log('showModalBtn');
    this.setData({ showModal: true });
    // 谢谢你帮我砍了{{ barginOwnData.cut_price/100 }}元
    wx.showToast({
      title: '谢谢你帮我砍了'+ this.data.barginOwnData.cut_price/100 + '元',
      icon: 'none',
      duration: 2000
    })
  },
  hideRuleModal: function () {
    this.setData({ showRuleModal: true });
  },
  hideModal: function () {
    this.setData({ showModal: false });
  },
  hideRuleModal: function () {
    this.setData({ showRuleModal: false });
  },
  goBargainRule: function () {
    this.setData({
      showRuleModal: true
    })
  }
  ,
  goBackHome: function() {
    wx.reLaunch({
      url: '/pages/active/bargainHomePage/bargainHomePage'
    });
  },
})
